package com.gxuwz.leave.servlet;

import com.gxuwz.leave.context.AppContext;
import com.gxuwz.leave.entity.Department;
import com.gxuwz.leave.entity.Instructor;
import com.gxuwz.leave.service.DepService;
import com.gxuwz.leave.service.InstService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet(name="InstServlet", urlPatterns = {"/inst"})
public class InstServlet extends BaseHttpServlet{

    private final static String VIEW_ADD = "/WEB-INF/views/instructor/inst_add.jsp";
    private final static String VIEW_EDIT = "/WEB-INF/views/instructor/inst_edit.jsp";
    private final static String VIEW_LIST = "/WEB-INF/views/instructor/inst_list.jsp";

    private final static String ACTION_LIST = "/inst?action=list";

    private InstService instService;

    private DepService depService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String action = req.getParameter("action");

        String view = ErrorPage_500;
        try{
            switch (action){
                //导向新增页面
                case "input":
                    view = VIEW_ADD;
                    Instructor inst = new Instructor();
                    List<Department> depList = depService.list(null);
                    req.setAttribute("depList", depList);
                    req.setAttribute("inst", inst);
                    break;
                //提交新增操作
                case "add":
                    inst = new Instructor();
                    inst.setInstID(req.getParameter("instID"));
                    inst.setInstName(req.getParameter("instName"));
                    inst.setPassword(req.getParameter("password"));
                    inst.setDepID(req.getParameter("depID"));
                    inst.setTelephone(req.getParameter("telephone"));
                    instService.addInst(inst);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                //编辑操作时，获取那一行的数据
                case "get":
                    String instID = req.getParameter("instID");
                    System.out.println("要编辑的是id为:"+instID+"的用户");
                    inst = instService.findInst(instID);
                    view = VIEW_EDIT;
                    depList = depService.list(null);
                    req.setAttribute("depList", depList);
                    req.setAttribute("inst", inst);
                    break;
                //提交编辑操作
                case "edit":
                    inst = new Instructor();
                    inst.setInstID(req.getParameter("instID"));
                    inst.setInstName(req.getParameter("instName"));
                    inst.setPassword(req.getParameter("password"));
                    inst.setDepID(req.getParameter("depID"));
                    inst.setTelephone(req.getParameter("telephone"));
                    instService.editInst(inst);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                //搜索
                case "search":
                    String keywords = req.getParameter("keywords");
                    inst = new Instructor();
                    inst.setInstID(keywords);
                    List<Instructor> list = instService.list(inst);
                    if(list.isEmpty()){
                        inst = new Instructor();
                        inst.setInstName(keywords);
                        list = instService.list(inst);
                    }
                    view = VIEW_LIST;
                    req.setAttribute("list", list);
                    break;
                //删除操作
                case "delete":
                    instID = req.getParameter("instID");
                    instService.deleteInst(instID);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                case "list":
                    List<Instructor> instList = instService.list(null);
                    view = VIEW_LIST;
                    req.setAttribute("list", instList);
                    break;
                default:
            }
        }catch (Exception e){
            e.printStackTrace();
            req.setAttribute("message", e.getMessage());
            req.setAttribute("operation", "failure");
            view = ACTION_LIST;
        }
        super.doDispatch(req,resp,view);
    }

    @Override
    public void init() throws ServletException {
        super.init();
        instService = AppContext.getBean(InstService.class);
        depService = AppContext.getBean(DepService.class);
    }
}
